package com.ruoyi.material.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.base.util.GenCodeUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.material.mapper.MaterialInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.material.domain.MaterialInfo;
import com.ruoyi.material.mapper.ReceiptMaterialInfoMapper;
import com.ruoyi.material.domain.ReceiptMaterialInfo;
import com.ruoyi.material.service.IReceiptMaterialInfoService;

/**
 * 收料单信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-25
 */
@Service
public class ReceiptMaterialInfoServiceImpl implements IReceiptMaterialInfoService {
    @Autowired
    private ReceiptMaterialInfoMapper receiptMaterialInfoMapper;

    @Autowired
    private MaterialInfoMapper materialInfoMapper;

    /**
     * 查询收料单信息
     *
     * @param id 收料单信息主键
     * @return 收料单信息
     */
    @Override
    public ReceiptMaterialInfo selectReceiptMaterialInfoById(Long id) {
        ReceiptMaterialInfo receiptMaterialInfo = receiptMaterialInfoMapper.selectReceiptMaterialInfoById(id);
        //按照物料类别，物料名称，收料单单号

        List<MaterialInfo> materialInfoList = receiptMaterialInfo.getMaterialInfoList();
        HashMap<String,MaterialInfo> materialInfoHashMap = new HashMap<>();
        for (MaterialInfo materialInfo : materialInfoList) {
            String key = materialInfo.getMaterialName() + "_" + materialInfo.getMaterialType() + "_" + materialInfo.getSpecifications() + "_" + materialInfo.getUnit();
            if (materialInfoHashMap.containsKey(key)){
                MaterialInfo info = materialInfoHashMap.get(key);
                info.setCount(materialInfo.getCount() + info.getCount());
                materialInfoHashMap.put(key,info);
            }else {
                materialInfoHashMap.put(key,materialInfo);
            }
        }
        materialInfoList.clear();
        materialInfoHashMap.forEach((k,v)->materialInfoList.add(v));
        receiptMaterialInfo.setMaterialInfoList(materialInfoList);
        return receiptMaterialInfo;
    }

    /**
     * 查询收料单信息列表
     *
     * @param receiptMaterialInfo 收料单信息
     * @return 收料单信息
     */
    @Override
    public List<ReceiptMaterialInfo> selectReceiptMaterialInfoList(ReceiptMaterialInfo receiptMaterialInfo) {
        List<ReceiptMaterialInfo> infos = new ArrayList<>();
        for (ReceiptMaterialInfo materialInfo : receiptMaterialInfoMapper.selectReceiptMaterialInfoList(receiptMaterialInfo)) {
            ReceiptMaterialInfo info = receiptMaterialInfoMapper.selectReceiptMaterialInfoById(materialInfo.getId());
            List<MaterialInfo> materialInfoList = info.getMaterialInfoList();
            if (materialInfoList.size() >0){
                info.setSummary(materialInfoList.get(0).getMaterialName() + "等" + materialInfoList.size() +"项");
            }
            infos.add(info);
        }
        return infos;
    }

    /**
     * 新增收料单信息
     *
     * @param receiptMaterialInfo 收料单信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertReceiptMaterialInfo(ReceiptMaterialInfo receiptMaterialInfo) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        receiptMaterialInfo.setCreateBy(user.getCreateBy());
        receiptMaterialInfo.setCreateTime(user.getCreateTime());
        receiptMaterialInfo.setReceiptMaterialNo(GenCodeUtils.getReceiptMaterialNo());
        receiptMaterialInfo.setCreateTime(DateUtils.getNowDate());
        int rows = receiptMaterialInfoMapper.insertReceiptMaterialInfo(receiptMaterialInfo);
        insertMaterialInfo(receiptMaterialInfo);
        return rows;
    }

    /**
     * 修改收料单信息
     *
     * @param receiptMaterialInfo 收料单信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateReceiptMaterialInfo(ReceiptMaterialInfo receiptMaterialInfo) {
        //维护编号
        ReceiptMaterialInfo info = receiptMaterialInfoMapper.selectReceiptMaterialInfoById(receiptMaterialInfo.getId());
        receiptMaterialInfo.setReceiptMaterialNo(info.getReceiptMaterialNo());

        SysUser user = SecurityUtils.getLoginUser().getUser();
        receiptMaterialInfo.setUpdateBy(user.getUserName());
        receiptMaterialInfo.setUpdateTime(DateUtils.getNowDate());
        receiptMaterialInfoMapper.deleteMaterialInfoByReceiptMaterialNo(receiptMaterialInfo.getReceiptMaterialNo());
        insertMaterialInfo(receiptMaterialInfo);
        return receiptMaterialInfoMapper.updateReceiptMaterialInfo(receiptMaterialInfo);
    }

    /**
     * 批量删除收料单信息
     *
     * @param ids 需要删除的收料单信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReceiptMaterialInfoByIds(Long[] ids) {
        receiptMaterialInfoMapper.deleteMaterialInfoByReceiptMaterialNos(ids);
        return receiptMaterialInfoMapper.deleteReceiptMaterialInfoByIds(ids);
    }

    /**
     * 删除收料单信息信息
     *
     * @param id 收料单信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReceiptMaterialInfoById(Long id) {
        ReceiptMaterialInfo receiptMaterialInfo = receiptMaterialInfoMapper.selectReceiptMaterialInfoById(id);
        receiptMaterialInfoMapper.deleteMaterialInfoByReceiptMaterialNo(receiptMaterialInfo.getReceiptMaterialNo());
        return receiptMaterialInfoMapper.deleteReceiptMaterialInfoById(id);
    }

    /**
     * 新增物料信息信息
     *
     * @param receiptMaterialInfo 收料单信息对象
     */
    public void insertMaterialInfo(ReceiptMaterialInfo receiptMaterialInfo) {
        List<MaterialInfo> materialInfoList = receiptMaterialInfo.getMaterialInfoList();
        String receiptMaterialNo = receiptMaterialInfo.getReceiptMaterialNo();
        if (StringUtils.isNotNull(materialInfoList)) {
            List<MaterialInfo> list = new ArrayList<MaterialInfo>();
            for (MaterialInfo materialInfo : materialInfoList) {
                materialInfo.setReceiptMaterialNo(receiptMaterialNo);
                list.add(materialInfo);
            }
            if (list.size() > 0) {
                receiptMaterialInfoMapper.batchMaterialInfo(list);
            }
        }
    }

    /**
     * 检查收料单是否已经入库，true已经入库，false未入库
     * @param id
     * @return
     */
    public boolean checkStatus(Long id){
        ReceiptMaterialInfo receiptMaterialInfo = receiptMaterialInfoMapper.selectReceiptMaterialInfoById(id);
        if ("0".equals(receiptMaterialInfo.getStatus())){
            return false;
        }
        return true;
    }

    /**
     * 入库操作
     * @param id
     * @return
     */
    @Transactional
    @Override
    public int warehousingOperation(Long id){
        //查询物料单
        ReceiptMaterialInfo receiptMaterialInfo = receiptMaterialInfoMapper.selectReceiptMaterialInfoById(id);
        for (MaterialInfo materialInfo : receiptMaterialInfo.getMaterialInfoList()) {
            //查询库存，确定物料是否存在
            Long wId = receiptMaterialInfoMapper.selectWarehouseInfo(materialInfo);
            if (wId >0){
                //更新
                receiptMaterialInfoMapper.updateWarehouseInfo(wId,materialInfo.getCount());
            }else{
                //插入
                receiptMaterialInfoMapper.insertWarehouseInfo(materialInfo,receiptMaterialInfo);
            }
        }
        //更新入料单
        receiptMaterialInfo.setStatus("1");
        return receiptMaterialInfoMapper.updateReceiptMaterialInfo(receiptMaterialInfo);
    }

    @Override
    public List<MaterialInfo> getAllocationDetails(Long id) {
        MaterialInfo materialInfo = materialInfoMapper.selectMaterialInfoById(id);
        if (materialInfo == null){
            throw new ServiceException("该物料信息不存在！") ;
        }
        //收料单，物料名称，类别，规则，单位
        MaterialInfo info = new MaterialInfo();
        info.setReceiptMaterialNo(materialInfo.getReceiptMaterialNo());
        info.setMaterialName(materialInfo.getMaterialName());
        info.setMaterialType(materialInfo.getMaterialType());
        info.setSpecifications(materialInfo.getSpecifications());
        info.setUnit(materialInfo.getUnit());
        return materialInfoMapper.selectMaterialInfoList(info);
    }
}
